<template>
  <div class="activity">
    <div class="tab-list flex-a">
      <div :class="tab == item.value ? 'active item' : 'item'" v-for="item in tabList" :key="item.value"
        @click="changeTab(item.value)">
        <span>{{ item.name }}</span>
        <i></i>
      </div>
    </div>
    <div class="box">
      <!-- 满减满折 -->
      <div v-if="tab == 'reduce'">
        <el-form :model="queryParams" ref="queryForm" class="input" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item>
            <el-select v-model="queryParams.state" placeholder="所有活动">
              <el-option v-for="item in stateList" :key="item.id" :label="item.name" :value="item.id + ''">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker v-model="queryParams.time" value-format="yyyy-MM-dd" type="daterange"
              :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
              align="right">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-input v-model="queryParams.actName" prefix-icon="el-icon-search" placeholder="请输入关键字" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="btn" size="mini" @click="handleQuery">查询</el-button>
            <el-button type="primary" class="btn" size="mini" @click="handleAdd">新增满减</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="activityList">
          <el-table-column label="名称" align="center" prop="actName" />
          <el-table-column label="开始时间" align="center" prop="beginDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="结束时间" align="center" prop="endDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否与会员折扣同享" align="center" prop="isDiscount">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.isDiscount == 1" size="medium" type="success" effect="dark">是</el-tag>
              <el-tag v-if="scope.row.isDiscount == 2" size="medium" type="info" effect="dark">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="满减门槛" align="center" prop="fullKey" />
          <el-table-column label="减少价格" align="center" prop="lessPrice" />
          <el-table-column label="满减折扣" align="center" prop="lessDiscount" />
          <el-table-column label="是否配置商品" align="center" prop="prodAdd">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.prodAdd == 1" size="medium" type="success" effect="dark">是</el-tag>
              <el-tag v-if="scope.row.prodAdd == 2" size="medium" type="info" effect="dark">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['pet:activity:edit']">编辑</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['pet:activity:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </div>
      <!-- 充值活动 -->
      <div v-if="tab == 'recharge'">
        <recharge></recharge>
      </div>
      <!-- 添加或修改活动对话框 -->
      <el-dialog :title="title" :visible.sync="open" class="center" width="500px" append-to-body>
        <el-form ref="form" :model="form" class="input" :rules="rules" label-width="110px">
          <el-form-item label="名称" prop="actName">
            <el-input v-model="form.actName" placeholder="请输入名称" />
          </el-form-item>
          <el-form-item label="开始时间" prop="beginDate">
            <el-date-picker clearable v-model="form.beginDate" style="width: 100%;" type="date" :picker-options="pickerOptionsStart"
              value-format="yyyy-MM-dd" placeholder="请选择开始时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间" prop="endDate">
            <el-date-picker clearable v-model="form.endDate" style="width: 100%;" type="date" :picker-options="pickerOptionsEnd" value-format="yyyy-MM-dd"
              placeholder="请选择结束时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="与会员折扣同享" prop="isDiscount">
            <el-radio v-model="form.isDiscount" label="1">是</el-radio>
            <el-radio v-model="form.isDiscount" label="2">否</el-radio>
          </el-form-item>
          <el-form-item label="满减门槛" prop="fullKey">
            <el-input v-model="form.fullKey" placeholder="请输入满减门槛" />
          </el-form-item>
          <el-form-item label="减少价格" prop="lessPrice">
            <el-input v-model="form.lessPrice" :disabled="disPrice" @change="inputPrice" class="input-radius-right" placeholder="请输入减少价格" >
              <template slot="append">元</template>
            </el-input>
          </el-form-item>
          <el-form-item label="满减折扣" prop="lessDiscount">
            <el-input v-model="form.lessDiscount" :disabled="disDiscount" @change="inputDiscount" class="input-radius-right" placeholder="请输入满减折扣">
              <template slot="append">折</template>
            </el-input>
          </el-form-item>
          <el-form-item label="配置商品" prop="prodAdd">
            <el-radio v-model="form.prodAdd" label="1">是</el-radio>
            <el-radio v-model="form.prodAdd" label="2">否</el-radio>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" class="btn" style="padding: 12px 70px;" @click="submitForm">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/pet/activity";
import recharge from './../recharge/index'
export default {
  name: "Activity",
  components:{recharge},
  data() {
    return {
      disPrice:false,
      disDiscount:false,
      tab: 'reduce',
      tabList: [
        {
          name: '满减满折',
          value: 'reduce'
        },
        {
          name: '充值活动',
          value: 'recharge'
        },
      ],
      stateList: [
        {
          id: 1,
          name: '未开始'
        },
        {
          id: 2,
          name: '进行中'
        },
        {
          id: 3,
          name: '已结束'
        },
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        actNo: null,
        actName: null,
        beginDate: null,
        endDate: null,
        isDiscount: null,
        fullKey: null,
        lessPrice: null,
        lessDiscount: null,
        prodAdd: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      pickerOptionsStart: {
        disabledDate: time => {
          const endDateVal = new Date(this.form.endDate).getTime()
          if (endDateVal) {
            return time.getTime() > endDateVal - 0
          }
        }
      },
      pickerOptionsEnd: {
        disabledDate: time => {
          const beginDateVal = new Date(this.form.beginDate).getTime()
          if (beginDateVal) {
            return time.getTime() < beginDateVal - 0
          }
        }
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询满减列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // tab切换
    changeTab(value) {
      this.tab = value
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        actNo: null,
        actName: null,
        beginDate: null,
        endDate: null,
        isDiscount: null,
        fullKey: null,
        lessPrice: null,
        lessDiscount: null,
        prodAdd: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增满减";
    },
    //输入满减价格
    inputPrice(v){
      if(v){
        this.disDiscount=true
        this.disPrice=false
      }else{
        this.disDiscount=false
      }
    },
    //输入满减折扣
    inputDiscount(v){
      if(v){
        this.disDiscount=false
        this.disPrice=true
      }else{
        this.disPrice=false
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除活动编号为"' + ids + '"的数据项？').then(function () {
        return delActivity(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
